"""
使用正则表达式把代码预处理成正常的 python语言

"""


class mid_type_name:
    def __init__(self, Type, name):
        self.Type = Type
        self.name = name
        self.opt = {}
    def __str__(self):
        Type, name = self.Type, self.name
        r = "{Type} {name}".format(**locals())
        if Type == None:   # ??
            return r
        if "[" in Type:  # c array
            ret, rest = Type.split("[", 1)
            r = "{ret} {name}[{rest}".format(**locals())

        if "(" in Type: # func
            ret, rest = Type.split("(", 1)
            r = "{ret} (*{name})({rest}".format(**locals())
        return r


class mid_func:
    def __init__(self, class_name, func_name, name_list, type_list, body_node, deco_list):
        self.class_name = class_name
        self.name = func_name
        #raw
        self.param_name_str = param_name_str
        self.param_type_str = param_type_str
        self.body_node = body_node
        self.deco_list = deco_list

        #parse
        self.Return = None
        self.param_list = []




class mid_class:
    def __init__(self, class_name):# , var_node, func_node_list, relys, macro_list, std):
        #pass1 type_space
        self.class_name = class_name
        self.relys = relys 
        self.var_init_node = []
        self.func_node_dict = {}
        self.std = ""
        self.macro_list []
        self.base_list = []

class node_visitor:
    def __init__(self, node, info=None):
        self.node = node
        self.info = {} if info = None else info
    def visit(self):
        return eval("self._{}()".format(self.node.__class__.__name__))
    def visit_sub(self, sub_node, info=None):
        sub_node = node_visitor(sub_node, info)
        return sub_node.visit()

class type_space_pass(node_visitor):
    def __init__(self, class_node, info):
        # info = {"mid_class":mid_class()}
        super().__init__(class_node, info)
    def _ClassDef(self):
        # ClassDef(identifier name,
        #      expr* bases,
        #      keyword* keywords,
        #      stmt* body,
        #      expr* decorator_list)
        class_name = self.trans(node.name)
        self.add_class(class_name)
        base_list = []
        for base in node.bases:
            base_list.append(self.trans(base))
        base_list = "\nbase_list_gap\n".join(base_list)
        
        body_list = []
        for sub_node in node.body:
            sub_node.ext_class_name = class_name
            body_list.append(self.trans(sub_node))
            #sub_node.ext_opt = "include"
        #body_list = "\nbody_list_gap\n".join(body_list)
        body_list = "\n".join(body_list)
        r = """
#ifndef __H_module_{class_name}_H__
#define __H_module_{class_name}_H__

{base_list}

{body_list}
#endif //__H_module_{class_name}_H__
""".format(**locals())
        return r

    
